package com.butnet.sqltools.connection;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

public class Config implements Serializable {
	private static final long serialVersionUID = 1L;
	private Map<UncaseString, String> maps = new LinkedHashMap<UncaseString, String>(){
		private static final long serialVersionUID = 1L;

		public String get(Object key) {
			for(Iterator<Entry<UncaseString, String>> it = getHeader(); it.hasNext(); ){
				Entry<UncaseString, String> en = it.next();
				if(key==null){
					if(en.getKey()==null)return en.getValue();
				}else{
					if(en.getKey()!=null&&en.getKey().equals(key))return en.getValue();
				}
			}
			return null;
		};
	};
	private String name = null;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String get(String name) {
		return maps.get(name);
	}

	public void set(String name, String value) {
		maps.put(new UncaseString(name), value);
	}

	public Iterator<Entry<UncaseString, String>> getHeader() {
		return maps.entrySet().iterator();
	}

	public void clear() {
		maps.clear();
	}
	
	@Override
	public String toString() {
		StringBuffer str = new StringBuffer();
		Set<Entry<UncaseString, String>> enSet = maps.entrySet();
		for(Iterator<Entry<UncaseString, String>> it = enSet.iterator(); it.hasNext(); ){
			Entry<UncaseString, String> en = it.next();
			str.append(en.getKey()+"= "+en.getValue()+"\n");
		}
		return str.toString();
	}

	public static boolean saveConfig(Config config, String confFile) {
		FileWriter out = null;
		BufferedWriter writer = null;
		try {
			out = new FileWriter(confFile);
			writer = new BufferedWriter(out);
			for (Iterator<Entry<UncaseString, String>> it = config.getHeader(); it.hasNext();) {
				Entry<UncaseString, String> en = it.next();
				writer.write(en.getKey() + "=" + en.getValue());
				writer.newLine();
			}
		} catch (IOException e) {
			e.printStackTrace(System.err);
			return false;
		} finally {
			try {
				if (writer != null)
					writer.close();
			} catch (Exception e) {
				e.printStackTrace(System.err);
			}
			try {
				if (out != null)
					out.close();
			} catch (IOException e) {
				e.printStackTrace(System.err);
			}
		}
		return true;
	}

	public static Config readConfig(String confFile) {
		Config conf = new Config();
		InputStream in = null;
		BufferedReader r = null;
		File confFileObj = new File(confFile);
		try {
			in = new FileInputStream(confFile);
			r = new BufferedReader(new InputStreamReader(in));
			String line = null;
			int i = -1;
			while ((line = r.readLine()) != null) {
				line = line.trim();
				if (line.length() == 0)
					continue;
				char c = line.charAt(0);
				switch (c) {
				case '#':
				case '!':
				case '/':
				case '\\':
					continue;
				}
				i = line.indexOf('=');
				if (i == -1)
					continue;
				String n = line.substring(0, i).trim();
				String v = line.substring(1 + i).trim();
				conf.maps.put(new UncaseString(n), v);
			}
		} catch (Exception e) {
			e.printStackTrace(System.err);
		} finally {
			try {
				if (r != null)
					r.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				if (in != null)
					in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		conf.setName(confFileObj.getName());
		return conf;
	}
}
